using System;
using System.Collections.Generic;
using System.Linq;
using Charisma.Tests.Models.BlogApp;
using NUnit.Framework;

namespace Charisma.Tests
{
  [TestFixture]
  public class RepositoryTests
  {
    [SetUp]
    public void SetUp()
    {
        var context = new BlogAppContext();
        context.Database.Drop();
//        context.
    }

    private static Repository<Blog> GetBlogRepository()
    {
      var context = new BlogAppContext();
      return context.Blogs;
    }

    [Test]
    public void AddTest()
    {
      var repo = GetBlogRepository();
      var blog = new Blog();
      repo.Add(blog);

      Assert.IsNotNullOrEmpty(blog.Id);
    }

    [Test]
    public void DoubleAddTest()
    {
      var repo = GetBlogRepository();
      var blog = new Blog();
      repo.Add(blog);
      Assert.Throws<CharismaException>(() => repo.Add(blog));
    }
  
    [Test]
    public void AddWithTheSameIdTest()
    {
      var repo = GetBlogRepository();
      var blog1 = new Blog()
        {
          Id = "00015"
        };
      repo.Add(blog1);
      var blog2 = new Blog()
        {
          Id = "00015"
        };
      Assert.Throws<CharismaException>(() => repo.Add(blog2));
    }

    [Test]
    public void AddRemoveTest()
    {
      var repo = GetBlogRepository();
      var blog = new Blog();
      repo.Add(blog);
      Assert.AreEqual(1, repo.All.ToList().Count());

      repo.Remove(blog);
      Assert.AreEqual(0, repo.All.ToList().Count());
    }

    [Test]
    public void DoubleRemoveTest()
    {
      var repo = GetBlogRepository();
      var blog = new Blog();
      repo.Add(blog);
      repo.Remove(blog);
      repo.Remove(blog);
    }

    [Test]
    public void RemoveWithoutKeyTest()
    {
      var repo = GetBlogRepository();
      var blog = new Blog();
      
      Assert.Throws<InvalidOperationException>(() => repo.Remove(blog));
    }

    [Test]
    public void RemoveWithoutAdditionTest()
    {
      var repo = GetBlogRepository();
      var blog = new Blog();
      blog.Id = "0045";
      
      repo.Remove(blog);
    }

    [Test]
    public void AddAndFindTest()
    {
      var repo = GetBlogRepository();
      var blog1 = new Blog();
      repo.Add(blog1);
      Assert.IsNotNull(blog1.Id);

      var blog2 = repo.Find(blog1.Id);
      Assert.IsNotNull(blog2);
      Assert.AreNotSame(blog1, blog2);
    }

    [Test]
    public void DoubleFindTest()
    {
      var repo = GetBlogRepository();
      var blog1 = new Blog();
      repo.Add(blog1);
      Assert.IsNotNull(blog1.Id);

      var blog2 = repo.Find(blog1.Id);
      Assert.IsNotNull(blog2);
      Assert.AreNotSame(blog1, blog2);

      var blog3 = repo.Find(blog1.Id);
      Assert.IsNotNull(blog3);
//      if (TestEnvironment.DbProvider.GetType() != typeof(DummyDb.DummyDbProvider))
//        Assert.AreNotSame(blog2, blog3);
    }

    
  }
}